package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"regexp"
)

func main() {

	srv := gin.Default()
	srv.POST("/sms/send", func(c *gin.Context) {
		phone := c.DefaultPostForm("Phone", "")

		if phone == "" {
			c.JSON(http.StatusOK, "手机号不能为空")
			return
		}

		if matched, _ := regexp.MatchString(`^[0-9]+$`, phone); !matched {
			c.JSON(http.StatusOK, "手机号必须数字")
			return
		}

		if matched, _ := regexp.MatchString(`^(1[3-9][0-9]\d{8})$`, phone); !matched {
			c.JSON(http.StatusOK, "手机号格式不正确")
			return
		}

		c.JSON(http.StatusOK, gin.H{"Phone": phone})
	})

	srv.POST("/user/sms/login", func(c *gin.Context) {
		phone := c.DefaultPostForm("Phone", "")
		code := c.DefaultPostForm("Code", "")

		if phone == "" {
			c.JSON(http.StatusOK, "手机号不能为空")
			return
		}

		if matched, _ := regexp.MatchString(`^[0-9]+$`, phone); !matched {
			c.JSON(http.StatusOK, "手机号必须数字")
			return
		}

		if matched, _ := regexp.MatchString(`^(1[3-9][0-9]\d{8})$`, phone); !matched {
			c.JSON(http.StatusOK, "手机号格式不正确")
			return
		}

		if code == "" {
			c.JSON(http.StatusOK, "验证码不能为空")
			return
		}

		if matched, _ := regexp.MatchString(`^[0-9]+$`, code); !matched {
			c.JSON(http.StatusOK, "验证码必须数字")
			return
		}

		if len(code) != 4 {
			c.JSON(http.StatusOK, "验证码必须数字")
			return
		}

		c.JSON(http.StatusOK, gin.H{"Phone": phone, "Code": code})
	})

	srv.Run(":80")
}
